<template>
  <xtxBread>
    <xtxBreadItem to="/">首页</xtxBreadItem>
    <xtxBreadItem v-if="category.top" :to="`/category/${category.top.id}`">{{ category.top.name }}</xtxBreadItem>
    <transition name="fade-right"  mode="out-in">
      <xtxBreadItem v-if="category.sub" :key="category.sub.id">{{ category.sub.name }}</xtxBreadItem>
    </transition>
</xtxBread>
</template>

<script>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
export default {
  name: 'SubBread',
  setup () {
    // 注意：setup中的this不是当前的vue实例

    // 1.获取二级分类的id 在地址路由中获取
    // route ===> this.$route
    const route = useRoute()
    console.log(route.params.id, '2')
    // 2.获取vuex中的类目数据
    const store = useStore()
    console.log(store, '3')
    // 3.通过计算属性得到二级类目的名称和ID 以及一级类目的名称和ID
    const category = computed(() => {
      const obj = {}

      store.state.category.list.forEach(top => {
        top.children && top.children.forEach(sub => {
          // 判断当前所点击的分类id是否与路由地址的id相同
          if (sub.id === route.params.id) {
            // 设置二级类目
            obj.sub = {
              id: sub.id,
              name: sub.name
            }
            obj.top = {
              id: top.id,
              name: top.name
            }
          }
        })
      })
      return obj
    })
    return { category }
  }
}
</script>

<style lang="less" scoped>

</style>
